--气刃兜割
local qirendouge = fk.CreateSkill {
    name = "mcdh_qirendouge",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

qirendouge:addEffect(fk.EventPhaseStart,{
    can_trigger = function (self, event, target, player, data)
        return player == target and player:hasSkill(self) and target.phase == Player.Play and not player:isKongcheng() and table.find(player.room:getOtherPlayers(player), function(p) return player:canPindian(p) end)
        --    and player:getMark("@mcdh_kairen") ~= 0 --没有开刃不能登龙（x）
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local targets = table.filter(player.room:getOtherPlayers(player), function(p) return player:canPindian(p) end)
        local tos = room:askToChoosePlayers(player,{targets= targets,min_num=1,max_num=1,prompt= "#mcdh_qirendouge-choose",skill_name=self.name,cancelable=true})
        if #tos > 0 then
            self.cost_data = tos[1]
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room=player.room
        local targ = self.cost_data
        local pindian = player:pindian({targ}, self.name)
        local testnum = mcdh_kairen_str2num(player:getMark("@mcdh_kairen"))
        if testnum > 2 then
            player:drawCards(testnum - 1, self.name)
        end
        if pindian.results[targ.id].winner then
            local winner, loser
            if pindian.results[targ.id].winner == player then
                winner = player
                loser = targ
            else
                winner = targ
                loser = player
            end
            if loser.dead then return end
            local subcards = {}
            local temcard
            temcard = room:getSubcardsByRule(pindian.fromCard, { Card.DiscardPile })
            if #temcard > 0 then
                for _, c in ipairs(temcard) do
                    table.insert(subcards,c)
                end
            end
            temcard = room:getSubcardsByRule(pindian.results[targ.id].toCard, { Card.DiscardPile })
            if #temcard > 0 then
                for _, c in ipairs(temcard) do
                    table.insert(subcards,c)
                end
            end
            room:useVirtualCard("slash",subcards, winner, {loser}, self.name)
        end
        if pindian.results[targ.id].winner ~= player then
            local num = mcdh_kairen_str2num(player:getMark("@mcdh_kairen"))
            if num > 0 then
                num = num - 1
                room:setPlayerMark(player,"@mcdh_kairen",mcdh_kairen_num2str(num))
            end
        end
    end,
})

Fk:loadTranslationTable {
    ["mcdh_qirendouge"] = "登龙",--气刃兜割
    [":mcdh_qirendouge"] = "出牌阶段开始时，你可以与一名角色拼点并摸X-1张牌。赢的角色将两张拼点牌当做一张【杀】对没赢的角色使用。若你没赢，则你失去1枚「开刃」标记",
    ["#mcdh_qirendouge-choose"] = "登龙：你可以与一名角色拼点并摸X-1张牌。赢的角色将两张拼点牌当做一张【杀】对没赢的角色使用。若你没赢，则你失去1枚「开刃」标记",
}

return qirendouge